/*  PLUTO JS -Scripting Web Server.
    Copyright (C) 2013 vivek gangadharan

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package pluto.js.api;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.logging.Level;
import org.mozilla.javascript.Function;
import static pluto.core.AppLogger.log;
import pluto.core.Host;

public class App {

    private Host host;

    public App(Host host) {
        this.host = host;
    }
    
     public App map(String urlPattern, Function function) {
        host.config().mapUrl(urlPattern, function);
        return this;
    }
     
    public App listen() throws UnknownHostException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException {
        if (!this.host.listening()) {
            host.start();
        }
        return this;
    }

    public App put(String key, Object value) throws IOException {
        this.host.cache().put(key, value);
        return this;

    }

    public Object get(String key) throws ClassNotFoundException, IOException {
        return this.host.cache().get(key);
    }

    
    public int port() {
        return host.config().port();
    }

    public InetAddress host() {
        return host.config().host();
    }

    public boolean allowDirectoryListing() {
        return this.host.config().allowDirListing();
    }

   
    public void remove(String key) {
        this.host.cache().remove(key);
    }
    

    public App log(String message) {
        log.log(Level.SEVERE, message);
        return this;
    }

    public App log(String message, Exception ex) {
        log.log(Level.SEVERE, message, ex);
        return this;
    }
    
    @Override
    public String toString() {
        return "App";
    }
}
